from PIL import Image
import math
import tkinter
import tkinter.messagebox

num = 10000
num2 = 20000
num3 = 30000
# 完成提示
def yes(re):
    tkinter.messagebox.showinfo("结果","转换成功,文件名为："+re)
def no():
    tkinter.messagebox.showerror("结果","转换失败")
# 转图片

def ip():
    global num
    global numl
    nm = en1.get()
    numl = str(num)+".bmp"
    try:
        def encode(text):
          str_len= len(text)
          width=math.ceil(str_len**0.5)
          im=Image. new("RGB",(width, width), 0x0)
          
          x,y=0,0
          for i in text:
            index= ord(i)
            rgb=(0, (index & 0xFF00)>> 8, index & 0xFF)
            im.putpixel((x, y),rgb)
            if x== width - 1:
               x=0
               y +=1
            else:
               x+=1
          return im

        if __name__=='__main__':
            im= encode (nm)
            im.save(numl)
            num += 1
    except:
        no()
    else:
        yes(numl)
# 转文字
def out():
    global num2
    global numl2
    numl2 = str(num2)+".txt"
    image = en2.get()
    try:
        def decode(im):
            width,height = im.size
            lst = []
            for y in range(height):
                for x in range(width):
                    red,green,blue = im.getpixel((x,y))
                    if(blue | green | red) == 0:
                        break
                    index = (green<<8) + blue
                    lst.append(chr(index))
            return ''.join(lst)

        if __name__=='__main__':

            all_text = decode(Image.open(image,"r"))
            with open (numl2,"w",encoding = "utf-8") as f:
                f.write(all_text)
    except:
        no()
    else:
        yes(numl2)
        num2 +=1

def ip2():
    global num3
    global numl3
    nm = en3.get()+".txt"
    numl3 = str(num3)+".bmp"
    try:
        def encode(text):
          str_len= len(text)
          width=math.ceil(str_len**0.5)
          im=Image. new("RGB",(width, width), 0x0)
          
          x,y=0,0
          for i in text:
            index= ord(i)
            rgb=(0, (index & 0xFF00)>> 8, index & 0xFF)
            im.putpixel((x, y),rgb)
            if x== width - 1:
               x=0
               y +=1
            else:
               x+=1
          return im

        if __name__=='__main__':
          with open(nm,encoding="utf-8")as f:
            all_text=f.read()
            
            im= encode (all_text)
            im.save(numl3)
    except:
        no()
    else:
        yes(numl3)
        num3 +=1
        
w = tkinter.Tk()
w.geometry("780x490")
w.title("转换器")
lb1 = tkinter.Label(w,text="需要转换的文本和图片请和程序放在同一文件夹下",font=("微软雅黑",25))
lb1.pack()
lb2 = tkinter.Label(w,text="请输入需要转换的内容",font=("微软雅黑",20))
lb2.pack()
en1 = tkinter.Entry(w)
en1.pack(pady=10)
bt1 = tkinter.Button(w,text = "转图片",font=("微软雅黑",20),padx=50,command=ip)
bt1.pack()
lb3 = tkinter.Label(w,text="请输入需要转换的图片（记得加后缀名）",font=("微软雅黑",20))
lb3.pack()
en2 = tkinter.Entry(w)
en2.pack(pady=10)
bt2 = tkinter.Button(w,text = "转文字",font=("微软雅黑",20),padx=50,command=out)
bt2.pack()
lb4 = tkinter.Label(w,text="请输入文件名(txt属性！)",font=("微软雅黑",20))
lb4.pack()
en3 = tkinter.Entry(w)
en3.pack(pady=10)
bt3 = tkinter.Button(w,text = "转图片",font=("微软雅黑",20),padx=50,command=ip2)
bt3.pack()
w.mainloop()
